<?php
/**
 * Created by PhpStorm.
 * User: Sunmer
 * Date: 2019/5/31 0031
 * Time: 11:23
 * Email:Sunmer@foxmail.com
 */

/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author    yuan1994 <tianpian0805@gmail.com>
 * @link      http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license   http://www.apache.org/licenses/LICENSE-2.0
 */

namespace app\api\controller;

use think\Model;
use think\Response;
use think\View;
use think\Request;
use think\Session;
use think\Db;
use think\Config;
use think\Loader;
use think\Exception;
use think\exception\HttpException;
use app\admin\logic\Pub as PubLogic;
use Endroid\QrCode\QrCode;

class BaseController
{
    /**
     * ajax数据返回，规范格式
     * @param array $data   返回的数据，默认空数组
     * @param string $msg   信息
     * @param int $code     错误码，0-未出现错误|其他出现错误
     * @param array $extend 扩展数据
     * JSON_UNESCAPED_SLASHES
     */
    function ajax_return($data = [], $msg = "", $code = 0, $extend = [])
    {
        $ret = ["code" => $code, "msg" => $msg, "data" => $data,];
        $ret = array_merge($ret, $extend);

        return Response::create($ret, 'json');
    }

    /**
     * 获取手机验证码
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function getCode(){
        //获取参数--手机号
        $phone = request()->param('mobile');
        if (empty($phone)){
            return self::ajax_return('null','参数不完整','201');
        }
        //验证手机号是否非法
        if (!preg_match('/^1[35789]\d{9}$/',$phone)){
            return self::ajax_return('null','手机号非法','201');
        }
        $code = 1234;
        if (DB::table('tp_code')->where('member_mobile',$phone)->find()){
            $row = ['member_mobile'=>$phone,'code'=>$code,'update_at'=>date('Y-m-d H:i:s',time())];
            DB::table('tp_code')->where('member_mobile',$phone)->update($row);
            $data = ['member_mobile'=>$phone];
            return self::ajax_return($data,'获取成功','200');
        }else{
            $row = ['member_mobile'=>$phone,'code'=>$code,'update_at'=>date('Y-m-d H:i:s',time())];
            DB::table('tp_code')
                ->insert($row);
            $data = ['member_mobile'=>$phone];
            return self::ajax_return($data,'获取成功','200');
        }

    }

    /**
     * 密码加密
     * @param $password
     * @return string
     */
    public function password($password){
        $str = md5('tkwz'.$password);
        return $str;
    }

    /**
     * 获取当前域名
     * @return string
     */
    function domain()
    {
        //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        return $domain;
    }

    /**
     * 功能：生成二维码
     * @param string $qrData 手机扫描后要跳转的网址
     * @param string $qrLevel 默认纠错比例 分为L、M、Q、H四个等级，H代表最高纠错能力
     * @param string $qrSize 二维码图大小，1－10可选，数字越大图片尺寸越大
     * @param string $savePath 图片存储路径
     * @param string $savePrefix 图片名称前缀
     */
    function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = 'qrcode')
    {
        if (!isset($savePath)) return '';
        //设置生成png图片的路径
        $PNG_TEMP_DIR = $savePath;

        //检测并创建生成文件夹
        if (!file_exists($PNG_TEMP_DIR)) {
            mkdir($PNG_TEMP_DIR);
        }
        $filename = $PNG_TEMP_DIR . 'test.png';
        $errorCorrectionLevel = 'L';
        if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
            $errorCorrectionLevel = $qrLevel;
        }
        $matrixPointSize = 4;
        if (isset($qrSize)) {
            $matrixPointSize = min(max((int)$qrSize, 1), 10);
        }
        if (isset($qrData)) {
            if (trim($qrData) == '') {
                die('data cannot be empty!');
            }
            //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
            $filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
            //开始生成
            \PHPQRCode\QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
        } else {
            //默认生成
            \PHPQRCode\QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
        }
        if (file_exists($PNG_TEMP_DIR . basename($filename)))
            return basename($filename);
        else
            return FALSE;
    }
}
